package com.itheima.smspringboot.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.smspringboot.common.Result;
import com.itheima.smspringboot.entity.Admin;
import com.itheima.smspringboot.exception.CustomerExpception;
import com.itheima.smspringboot.service.AdminService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private final AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }


    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        Admin dbadmin = adminService.login(admin);
        if ( dbadmin!= null && dbadmin.getPassword().equals(admin.getPassword())) {
            return Result.success(dbadmin);
       }else {
            throw new CustomerExpception("用户名或者密码错误");
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin) {

        adminService.register(admin);

        return Result.success();


    }


    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
       adminService.add(admin);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();

    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Admin> adminList) {
       adminService.deleteBatch(adminList);
        return Result.success();

    }


    @GetMapping("/selectAll")
    public Result selectAll(Admin admin) {
        List<Admin> adminList = adminService.selectAllAdmin(admin);
        return Result.success(adminList);
    }


    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Admin admin){
        PageInfo pageInfo = adminService.selectPage(pageNum, pageSize,admin);
        return Result.success(pageInfo);
    }




}
